package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.common.StringUtil;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.service.ChanQianSCSFService;
import com.jy.rhin.infra.cda.service.ChanQianSFService;
import com.jy.rhin.infra.cda.ycf.ChanQianSCSFVo;
import com.jy.rhin.infra.cda.ycf.YunChanFBJKVo;
import com.jy.rhin.model.ChanQianSCSF;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.fuyou.*;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
public class ChanQianSCSFApplication {


    private static final String FTL_PATH = "/05chanqianscsf.ftl";

    @Resource
    private ChanQianSCSFService chanQianSCSFService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;
    @Resource
    private ChanQianSFService chanQianSFService;
	@Resource
	private HospitalAPI hospitalAPI;
    public String create(String bh,String fUWUJGBH) {
        return cdaCreator.create(createFS(bh,fUWUJGBH), "05chanqianscsf.ftl");
    }

    public ClinicalDocumentData createFS(String cHANHOU42TJKJCJLBH,String fUWUJGBH) {
        ChanQianSCSFVo info = chanQianSCSFService.queryOne(fUWUJGBH,cHANHOU42TJKJCJLBH);
        if(Objects.isNull(info)){
            throw new CDAException("主业务数据不存在",null);
        }
		HospitalDTO hospital = hospitalAPI.info(info.getFUWUJGBH()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(info.getFUWUJGBH()).getData();
        if(Objects.isNull(hospital)){
            throw new CDAException("机构数据不存在",null);
        }

        ChanQianSCSF docData = new ChanQianSCSF();
        setClinicalDocumentData(docData, info, hospital);
        docData.setParticipant(createParticipant(info));
        docData.setYunChanShi(createYunChanShi(info));
        docData.setJiWangShi(createJiWangShi(info));
        docData.setJianKangPG(createJianKangPG(info));
        docData.setJiaZuShi(createJiaZuShi(info));
        docData.setShengMingTZ(createShengMingTZ(info));
        docData.setXinZang(createXinZang(info));
        docData.setHuXi(createHuXi(info));
        docData.setShengZhiQi(createShengZhiQi(info));
        docData.setChanQianSYS(createChanQianSYS(info));
        docData.setChanQianFZJC(createChanQianFZJC(info));
        docData.setJianKangZD(createJianKangZD(info));
        docData.setXiaCiSF(createXiaCiSF(info));
        docData.setZhuanZhenJY(createZhuanZhenJY(info));
        return docData;
    }
    private JiWangShi createJiWangShi(ChanQianSCSFVo info){
    	JiWangShi data = new JiWangShi();
    	/*List<JieChuYHLB> yhlb = new LinkedList<JieChuYHLB>();
    	JieChuYHLB l = new JieChuYHLB();
    	l.setJieChuYHLB(new CodeObject(info.getGERENSDM(),info.getGERENSMC()));
    	yhlb.add(l);
    	data.setGeRenJCYHLB(yhlb);*/
    	List<String> jibingshis = new LinkedList<String>();
    	jibingshis.add(info.getJIWANGSMC());
    	data.setJiBingShi(jibingshis);
    	data.setShouShuSBZ("1".equals(info.getFUKESSSBZ())?"true":"false");
    	List<String> shoushus = new LinkedList<String>();
		if(StringUtil.isNullOrEmpty(info.getFUKESSSXS())){
			shoushus.add("无");
		}else{
			shoushus.add(info.getFUKESSSXS());
		}

    	data.setShouShuSMS(shoushus);
    	return data;
    }
    
    private ChanQianSCSFShengZhiQi createShengZhiQi(ChanQianSCSFVo info){
    	ChanQianSCSFShengZhiQi data = new ChanQianSCSFShengZhiQi();
    	data.setWaiYinYCBZ("2".equals(info.getWAIYINYCDM())?"true":"false");
    	data.setWaiYinYCMS(info.getWAIYINYCMC());
    	data.setFuJianYCBZ("2".equals(info.getFUJIANYCDM())?"true":"false");
		if(StringUtil.isNullOrEmpty(info.getFUJIANYCMS())){
			data.setFuJianYCMS("无");
		}else{
			data.setFuJianYCMS(info.getFUJIANYCMS());
		}
    	data.setGongTiQK(info.getZIGONGYCMC());
    	data.setGongTiYCBZ("2".equals(info.getZIGONGYCDM())?"true":"false");
    	data.setGongJingMS(info.getGONGJINGYCMC());
    	data.setGongJingYCBZ("2".equals(info.getGONGJINGYCDM())?"true":"false");
    	data.setYinDaoYCBZ("2".equals(info.getYINDAOYCDM())?"true":"false");
    	data.setYinDaoYCMS(info.getYINDAOYCMC());
    	return data;
    }
    
    private HuXi createHuXi(ChanQianSCSFVo info){
    	HuXi data = new HuXi();
    	data.setYiChangBZ("2".equals(info.getFEIBUYCDM())?"true":"false");
    	data.setYiChangMS(StringUtil.isNullOrEmpty(info.getFEIBUYCMC())?"未见异常":info.getXINZANGYCMC());
    	return data;
    }
    
    private XinZang createXinZang(ChanQianSCSFVo info){
    	XinZang data = new XinZang();
    	data.setYiChangBZ("2".equals(info.getXINZANGYCDM())?"true":"false");
    	data.setYiChangMS(StringUtil.isNullOrEmpty(info.getXINZANGYCMC())?"未见异常":info.getXINZANGYCMC());
    	return data;
    }
    private List<JiaZuShi> createJiaZuShi(ChanQianSCSFVo info){
    	List<JiaZuShi> data = new ArrayList<JiaZuShi>();
    	return data;
    }
    private Participant createParticipant(ChanQianSCSFVo info){
    	Participant data = new Participant();
		if(info.getZHANGFUCSRQ()!=null){
			data.setBirthTime(CDAUtil.toD8(info.getZHANGFUCSRQ()));
		}
    	data.setName(info.getZHANGFUXM());
    	data.setTelecom(info.getZHANGFUDHHM());
    	return data;
    }
    private YunChanShi createYunChanShi(ChanQianSCSFVo info){
    	YunChanShi data = new YunChanShi();
    	data.setChanCi(info.getCHANCI()+"");
    	data.setChuShengQXLS(info.getCHUSHENGQXES()+"");
    	data.setLiuChanCS(String.valueOf(info.getZIRANLCS()+info.getRENGONGLCS()));
    	data.setMoCiYJRQ(CDAUtil.toD8(info.getMOCIYJRQ()));
    	data.setMouCiYJMQBZ(info.getMOCIYJRQ() == null? "false":"true");
    	data.setPaoGongCS(info.getPOUGONGCCS()+"");
    	data.setSiChanLS(info.getSICHANS()+"");
    	data.setSiTaiLS(""+info.getSITAIS());
    	data.setXinShengESWLS(info.getXINSHENGESWS()+"");
    	data.setYinDaoFMCS(info.getYINDAOFMCS()+"");
    	data.setYuChanQi(CDAUtil.toD8(info.getYUCHANQ()));
    	data.setYunCi(info.getYUNCI()+"");
    	return data;
    }
    
    private ChanQianFZJC createChanQianFZJC(ChanQianSCSFVo info){
    	ChanQianFZJC data = new ChanQianFZJC();
    	return data;
    }
    
    private ChanQianSCSFSYS createChanQianSYS(ChanQianSCSFVo info){
    	ChanQianSCSFSYS data = new ChanQianSCSFSYS();
    	data.setNiaoDanBJG(new CodeObject(info.getNIAODANBJGDM(), info.getNIAODANBJGMC()));
    	data.setXueHongDB(""+info.getXUEHONGDB());
    	data.setNiaoDanBJCZ(info.getNIAODANBDLJCZ().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		if(info.getXUEXIAOBJSZ() == null){
			data.setXueXiaoBan("10");
		}else{
			data.setXueXiaoBan(""+info.getXUEXIAOBJSZ());
		}
    	data.setBaiXiBao(info.getBAIXIBJSZ()+".00");
    	data.setNiaoTangLiang(info.getNIAOTANGDLJC().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    	data.setNiaoTongTi(new CodeObject(info.getNIAOTONGJGDM(), info.getNIAOTONGJGMC()));
    	data.setNiaoTang(new CodeObject(info.getNIAOTANGJGDM(), info.getNIAOTANGJGMC()));
    	data.setNiaoQianXue(new CodeObject(info.getNIAOQIANXJGDM(), info.getNIAOQIANXJGMC()));
		if(info.getKONGFUXT()!=null){
			data.setXueTang(info.getKONGFUXT().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		}

		if("1".equals(info.getMEIDULXTKYXQXJGDM()) ||  "2".equals(info.getMEIDULXTKYXQXJGDM())){
			data.setXueQing(new CodeObject(info.getMEIDULXTKYXQXJGDM(), info.getMEIDULXTKYXQXJGMC()));
		}else if("3".equals(info.getMEIDULXTKYXQXJGDM())){
			data.setXueQing(new CodeObject("2", "阳性"));
		}else{
			data.setXueQing(new CodeObject("1", "阴性"));
		}

		if("1".equals(info.getHIVKTDM()) ||  "2".equals(info.getHIVKTDM())){
			data.setHiv(new CodeObject(info.getHIVKTDM(), info.getHIVKTMC()));
		}else if("3".equals(info.getHIVKTDM())){
			data.setHiv(new CodeObject("2", "阳性"));
		}else{
			data.setHiv(new CodeObject("3", "不确定"));
		}

    	data.setBingZhuanAM(info.getXUEQINGGBZAM().setScale(0, BigDecimal.ROUND_HALF_UP).toString());
    	data.setCaoZhuanAM(info.getXUEQINGGCZAM().setScale(0, BigDecimal.ROUND_HALF_UP).toString());
    	data.setBaiDanBai(info.getBAIDANB().setScale(0, BigDecimal.ROUND_HALF_UP).toString());
		if(info.getZONGDANHS()!=null){
			data.setZongDanHS(info.getZONGDANHS().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		}
		if(info.getJIEHEDHS()!=null){
			data.setJieHeDHS(info.getJIEHEDHS().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		}
		if(info.getXUEQINGJG()!=null){
			data.setXueQingJi(info.getXUEQINGJG().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		}
		if(info.getXUENIAOSD()!=null){
			data.setXueNiaoSD(info.getXUENIAOSD().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
		}
    	data.setABO(new CodeObject(info.getABOXXDM()+"",info.getABOXXMC()));

		if("1".equals(info.getRHXXDM()+"")){
			info.setRHXXMC("阴性");
		} else if ("2".equals(info.getRHXXDM()+"") ){
			info.setRHXXMC("阳性");
		} else if ("3".equals(info.getRHXXDM()+"")) {
			info.setRHXXMC("不详");
		}else{
			info.setRHXXMC("未查");
		}

		data.setRH(new CodeObject(info.getRHXXDM()+"",info.getRHXXMC()));

    	data.setYinDaoFMW(new CodeObject(info.getYINDAOFMWDM(),info.getYINDAOFMWMC()));
    	data.setYinDaoQJD(new CodeObject(info.getYINDAOQJDDM(),info.getYINDAOQJDMC()));

		if("1".equals(info.getYIGANBMKYDM()) ||  "2".equals(info.getYIGANBMKYDM())){
			data.setYiGanKY(new CodeObject(info.getYIGANBMKYDM(),info.getYIGANBMKYMC()));
		}else if("3".equals(info.getYIGANBMKYDM())){
			data.setYiGanKY(new CodeObject("2", "阳性"));
		}else{
			data.setYiGanKY(new CodeObject("1", "阴性"));
		}

		if("1".equals(info.getYIGANBMKTDM()) ||  "2".equals(info.getYIGANBMKTDM())){
			data.setYiGanKT(new CodeObject(info.getYIGANBMKTDM(),info.getYIGANBMKTMC()));
		}else if("3".equals(info.getYIGANBMKTDM())){
			data.setYiGanKT(new CodeObject("2", "阳性"));
		}else{
			data.setYiGanKT(new CodeObject("1", "阴性"));
		}

		if("1".equals(info.getYIGANEKTDM()) ||  "2".equals(info.getYIGANEKTDM())){
			data.setYiGanEKT(new CodeObject(info.getYIGANEKTDM(),info.getYIGANEKTMC()));
		}else if("3".equals(info.getYIGANEKTDM())){
			data.setYiGanEKT(new CodeObject("2", "阳性"));
		}else{
			data.setYiGanEKT(new CodeObject("1", "阴性"));
		}

		if("1".equals(info.getYIGANEKYDM()) ||  "2".equals(info.getYIGANEKYDM())){
			data.setYiGanEKY(new CodeObject(info.getYIGANEKYDM(),info.getYIGANEKYMC()));
		}else if("3".equals(info.getYIGANEKYDM())){
			data.setYiGanEKY(new CodeObject("2", "阳性"));
		}else{
			data.setYiGanEKY(new CodeObject("1", "阴性"));
		}

		if("1".equals(info.getYIGANHXKTDM()) ||  "2".equals(info.getYIGANHXKTDM())){
			data.setYiGanHXKT(new CodeObject(info.getYIGANHXKTDM(),info.getYIGANHXKTMC()));
		}else if("3".equals(info.getYIGANHXKTDM())){
			data.setYiGanHXKT(new CodeObject("2", "阳性"));
		}else{
			data.setYiGanHXKT(new CodeObject("1", "阴性"));
		}
    	return data;
    }
    
    private JianKangPG createJianKangPG(ChanQianSCSFVo info){
    	JianKangPG data = new JianKangPG();
    	data.setYiChangBZ("2".equals(info.getBAOJIANZDLBDM())?"true":"false");
    	data.setYiChangMS(info.getYUNCHANFYCJG());
    	return data;
    }
    
    
    private List<ChanQianJianKangZD> createJianKangZD(ChanQianSCSFVo info){
    	List<ChanQianJianKangZD> list = new ArrayList<ChanQianJianKangZD>();
    	String[] dms = info.getBAOJIANZDLBDM().split("\\|");
    	for(int i=0;i<dms.length;i++){
    		ChanQianJianKangZD data = new ChanQianJianKangZD();
    		data.setZhiDao(new CodeObject(dms[i],info.getBAOJIANZDLBMC().split("\\|")[i]));
        	list.add(data);
    	}
    	return list;
    
    }
    
    
    private ChanQianSCSFShengMingTZ createShengMingTZ(ChanQianSCSFVo info){
    	ChanQianSCSFShengMingTZ data = new ChanQianSCSFShengMingTZ();
    	data.setShouSuoYa(String.valueOf(info.getSHOUSUOY()));
    	data.setShuZhangYa(String.valueOf(info.getSHUZHANGY()));
    	data.setTiZhong(info.getTIZHONG().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
    	data.setShenGao(info.getSHENGAO()+".0");
    	data.setTiZhiZS(info.getTIZHONGZS().toString());
    	return data;
    }
    
    private XiaCiSF createXiaCiSF(ChanQianSCSFVo info){
    	XiaCiSF data = new XiaCiSF();
    	data.setXiaCiSFSJ(CDAUtil.toD8(info.getXIACISFRQ()));
    	return data;
    }
    
    private FYZhuanZhenJY createZhuanZhenJY(ChanQianSCSFVo info){
    	FYZhuanZhenJY data = new FYZhuanZhenJY();
    	data.setZhuanZhenBZ("1".equals(info.getZHUANZHENBZ())?"true":"false");
    	data.setZhuanZhenYY(info.getZHUANZHENYY());
    	data.setZhuanRuJG(info.getZHUANRUJGMC());
    	data.setZhuanRuKS(info.getKESHIMC());
    	return data;
    }


    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,ChanQianSCSFVo info, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD8(info.getCHANQIANJCRQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(info));
        clinicalDocumentData.setAuthor(createAuthor(info, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(String.valueOf(info.getId()));
        clinicalDocumentData.setVersionNumber("1.0");
        return clinicalDocumentData;
    }
    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(ChanQianSCSFVo info, HospitalDTO hospital) {
        Author author = new Author();
        author.setId("021");
        author.setTime(CDAUtil.toD8(info.getCHANQIANJCRQ()));
        author.setName(info.getSUIFANGYSXM());
        author.setOrganizationId(hospital.getOutId());
        author.setOrganizationName(hospital.getName());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(ChanQianSCSFVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(info.getGERENDABS()));
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }


    Patient createPatient(ChanQianSCSFVo info) {
        Patient patient = new Patient();
        YunChanFBJKVo bingrenxx = chanQianSFService.getBJK( info.getYUNCHANFWYBS());
        if(bingrenxx == null){
        	patient.setName("未知");
        }else{
        	patient.setName(bingrenxx.getXINGMING());
        }
        patient.setId(info.getZHENGJIANHM());
        return patient;
    }

}
